package il.ac.braude.sona.control;
import il.ac.braude.sona.constants.ButtonPermission;
import il.ac.braude.sona.constants.Mssg;
import il.ac.braude.sona.constants.P;
import il.ac.braude.sona.constants.Resources;
import il.ac.braude.sona.entities.ChangeRequest;
import il.ac.braude.sona.gui.forms.FormFrame;
import il.ac.braude.sona.gui.forms.FormListener;
import il.ac.braude.sona.gui.forms.myforms.MakeDecisionForm;
import il.ac.braude.sona.gui.infogui.RequsetInfoFrame;
import il.ac.braude.sona.ocsf.client.Client;
import il.ac.braude.sona.ocsf.client.ServerResponseListener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JOptionPane;


/**
 * The BoardDecisionController contains all methods to handle board members actions
 * all board members can initiate decision, board chairman can approve it.
 *  
 * @author Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 */

public class BoardDecisionController implements FormListener{
	/**
	 * The form to set board decision.
	 */
	private MakeDecisionForm makeDecision;
	/**
	 * The form to approve board decision.
	 */
	private MakeDecisionForm approveDecision;
	/**
	 * Board decision to view/change by board chairman.
	 */
	private String decision;
	/**
	 * The stream to handle data going to the server.
	 */
	private RequsetInfoFrame  reqFram;
	/**
	 * The request info fram
	 */
	private ChangeRequest myReq;

	
	@Override
	public void onFormCanceled(FormFrame formFrame) {
		if (formFrame==makeDecision) {
			makeDecision.dispose();
			makeDecision=null;
		}
		if (formFrame==approveDecision) {
			approveDecision.dispose();
			approveDecision=null;
		}
		
	}
	
	@Override
	public void onFormSubmited(final FormFrame formFrame, ArrayList<Object> values) {
		if (formFrame==makeDecision){	
			if ((!values.contains(Resources.Strings.Execute)&&!values.contains(Resources.Strings.Reject))
				|| (values.contains(Resources.Strings.Execute)&&values.contains(Resources.Strings.Reject))){
				makeDecision.showMessege("CHOOSE ONE OPTION!");//check
				values.clear();
			}
			else{
			try {
				Client c = Client.getInstance();
				if ((values.get(0)==null))
					values.set(0, values.get(1));
				values.add(myReq.getID());
				Mssg mssg = new Mssg(ButtonPermission.MakeDecision,values);
				c.sendToServer(new ServerResponseListener() {
					@Override
					public void onServerResponse(Mssg mssg) {
						if (mssg.is(P.error)) {
							makeDecision.showMessege((String) mssg.getMessege());
							return;
						}
						JOptionPane.showMessageDialog(makeDecision,Resources.Strings.DesicionUpdated);
						formFrame.dispose();
						makeDecision.dispose();
						reqFram.dispose();
					}
				},mssg);
			 
			} catch (Exception e) {
				makeDecision.showMessege(Resources.Strings.ConnectFailed);
			}
			}
		}
			
		
		else if (formFrame==approveDecision){
			try {
			Client c = Client.getInstance();
			values.add(decision.toUpperCase());
			values.add(myReq.getID());
			Mssg mssg=new Mssg(ButtonPermission.ApproveBoardDecision,values);
			c.sendToServer(new ServerResponseListener() {
				@Override
				public void onServerResponse(Mssg mssg) {
					if (mssg.is(P.error)) {
						approveDecision.showMessege((String) mssg.getMessege());
						return;
					}
					else {
						JOptionPane.showMessageDialog(approveDecision,Resources.Strings.DesicionApproved);
						formFrame.dispose();
						formFrame.dispose();
						reqFram.dispose();
					}	
				}
			},mssg);
		 
		} catch (Exception e) {
			approveDecision.showMessege(Resources.Strings.ConnectFailed);
		}}

	}
	
	
/**
 * When BoardChairman wants to approve board decision, 
 * execApproveDecision Gets the Board decision from DB, create a decision form and viewing it.
 * allso handles change decision button.
 * 
 * @param req - the change request involved
 * @param requsetInfoFrame - the request info fram
 */
	
	public void execApproveDecision(ChangeRequest req,RequsetInfoFrame requsetInfoFrame) {
		reqFram=requsetInfoFrame;
		myReq=req;
		try {
			Client c = Client.getInstance();
			Mssg mssg= new Mssg(P.GetBoardDesision,req.getID());
			c.sendToServer(new ServerResponseListener() {
				@Override
				public void onServerResponse(Mssg mssg) {
					if (mssg.is(P.error)) {
						JOptionPane.showMessageDialog(null,"cant get decision from DB");
						return;
					}
					else {
						decision=new String((String)mssg.getMessege());
						approveDecision=new MakeDecisionForm(myReq.getID(),decision,new ActionListener() {
						@Override
						public void actionPerformed(ActionEvent e) {
							if (decision.toUpperCase().equals("EXECUTE"))
								decision="REJECT";
							else decision="EXECUTE";	
							approveDecision.changeDecision(myReq.getID(), decision,this);
							//approveDecision.addFormListener(BoardDecisionController.this);
							approveDecision.showFrame();
						}});
						approveDecision.addFormListener(BoardDecisionController.this);
						approveDecision.showFrame();
					}
				}
			},mssg);
		 
		} catch (Exception e) {
			JOptionPane.showMessageDialog(approveDecision,"cant get decision from DB");
		}
		
	}
	
	/**
	 * When Board member wants to update board decision, 
	 * execMakeDecision initiate new MakeDecisionForm and viewing it to B.member
	 * 
	 * @param req - the change request involved
	 * @param requsetInfoFrame - the request info fram
	 */
	public void execMakeDecision(ChangeRequest req,RequsetInfoFrame requsetInfoFrame) {
		myReq=req;
		reqFram=requsetInfoFrame;
		makeDecision=new MakeDecisionForm(req.getID());
		makeDecision.addFormListener(BoardDecisionController.this);
		makeDecision.showFrame();		
	}


}
